package com.kk.controller;

import com.alibaba.fastjson.JSONObject;
import com.kk.common.Result;
import com.kk.dao.RoleDao;
import com.kk.model.Role;
import com.kk.service.RoleService;
import com.kk.service.UserService;
import com.kk.util.ToolUtils;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色控制器
 *
 * @author fengshuonan
 *  2017年2月12日21:59:14
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    private static String PREFIX = "system/role";

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    /**
     * 跳转到角色列表页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/role";
    }

    /**
     * 跳转到添加角色
     */
    @RequestMapping(value = "/role_add")
    public String roleAdd() {
        return PREFIX + "/role_add";
    }

    /**
     * 跳转到修改角色
     */
    @RequestMapping(value = "/role_edit/{roleId}")
    public String roleEdit(@PathVariable String roleId, Model model) {
        if (ToolUtils.isEmpty(roleId)) {
            throw new ServiceException("请求参数为空");
        }
        Role role = this.roleService.findById(roleId);
        model.addAttribute(role);
        return PREFIX + "/role_edit";
    }

    /**
     * 跳转到角色分配
     */
    @RequestMapping(value = "/role_assign/{roleId}")
    public String roleAssign(@PathVariable("roleId") String roleId, Model model) {
        if (ToolUtils.isEmpty(roleId)) {
            throw new ServiceException("请求参数为空");
        }
        model.addAttribute("roleId", roleId);
        Role role = this.roleService.findById(roleId);
        model.addAttribute("roleName", role.getName());
        return PREFIX + "/role_assign";
    }

    /**
     * 获取角色列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(@RequestParam(required = false) String roleName) {
        List<Role> roles;
        if (ToolUtils.isNotEmpty(roleName)) {
            roles = this.roleService.findByName(roleName);
        }else{
           roles = this.roleService.findAll();
        }
        return Result.success(roles);
    }

    /**
     * 角色新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result add(@RequestBody Role role, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException("请求参数为空");
        }
        role.setId(null);

        //TODO
      this.roleService.save(role);
        return Result.success("操作成功");
    }

    /**
     * 角色修改
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Result edit(@RequestBody Role role, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException("请求参数为空");
        }
        this.roleService.save(role);

        return Result.success("操作成功");
    }

    /**
     * 删除角色
     */
    @RequestMapping(value = "/remove")
    @ResponseBody
    public Result remove(@RequestBody JSONObject json) {
        String roleId = json.getString("roleId");
        if (ToolUtils.isEmpty(roleId)) {
            throw new ServiceException("请求参数为空");
        }
        this.roleService.deleteById(roleId);
        return Result.success("操作成功");
    }

    /**
     * 查看角色
     */
    @RequestMapping(value = "/view/{roleId}")
    @ResponseBody
    public Result view(@PathVariable String roleId) {
        if (ToolUtils.isEmpty(roleId)) {
            throw new ServiceException("请求参数为空");
        }
        this.roleService.findById(roleId);
        return Result.success("操作成功");
    }

}
